package com.mobinters.image;

import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Bitmap.Config;
import android.util.Log;

public class Utils {
    public static void CopyStream(InputStream is, OutputStream os)
    {
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            for(;;)
            {
              int count=is.read(bytes, 0, buffer_size);
              if(count==-1)
                  break;
              os.write(bytes, 0, count);
            }
        }
        catch(Exception ex){}
    }
    
    public static Bitmap createBitmap(int color,int width,int height){
		Log.e("color",""+color);
		int size=width*height;
		int[] colorList=new int[size];
		for(int i=0;i<size;i++)colorList[i]=color;
		return Bitmap.createBitmap(colorList, width, height, Config.ARGB_8888);
	}
    
    
    public static Bitmap trimBitmap(Bitmap bmp,int nWidth,int nHeight){
		if(bmp==null) return null;
		float k1=(float)nWidth/bmp.getWidth();
		float k2=(float)nHeight/bmp.getHeight();
		Matrix mat=new Matrix();
		float scale =Math.max(k1, k2);
		mat.postScale(scale, scale);
		
		Bitmap bmp2=Bitmap.createBitmap(bmp, 0	, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
		bmp2=Bitmap.createBitmap(bmp2, 0, 0, nWidth, nHeight);
		return bmp2;
	}
    
    public static Bitmap overlayMark(Bitmap bmp1, Bitmap bmp2) //bmp1 is bigger
	{ 
		int height1= bmp1.getHeight();
		int width1= bmp1.getWidth();
		 
		int height2= bmp2.getHeight();
		int width2= bmp2.getWidth();
		Bitmap bmp3=bmp2;
		if(height2!=height1 && width2!=width1){
			bmp3=Bitmap.createScaledBitmap(bmp2, width1,height1,true);
		}
	    Bitmap bmOverlay = Bitmap.createBitmap(width1, height1, bmp1.getConfig()); 
	    Canvas canvas = new Canvas(bmOverlay); 
	    canvas.drawBitmap(bmp1, 0, 0, null);
	    canvas.drawBitmap(bmp3, 0, 0, null);
	    
	    return bmOverlay; 
	} 
	
	public static Bitmap overlayMark2(Bitmap bmp1, Bitmap bmp2) //bmp1 is bigger
	{ 
		int height1= bmp1.getHeight();
		int width1= bmp1.getWidth();	 
		
		int height2= bmp2.getHeight();
		int width2= bmp2.getWidth();
		
	    Bitmap bmOverlay = Bitmap.createBitmap(width1, height1, bmp1.getConfig()); 
	    Canvas canvas = new Canvas(bmOverlay); 
	    canvas.drawBitmap(bmp1, 0, 0, null);
	    canvas.drawBitmap(bmp2, (width1-width2)/2, (height1-height2)/2, null);
	    return bmOverlay; 
	} 
	
	public static Bitmap zoomBitmap(Bitmap source,int tW,int tH){
		//zoom bmp -->tW ,if nH<tH return, if nH>tH,crop
		Matrix matrix = new Matrix();
		float scale=(float)tW/source.getWidth();
		matrix.postScale(scale,scale);
		
		Bitmap bmp=Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, false);
		if(bmp!=null &&  bmp.getHeight()>tH) bmp=Bitmap.createBitmap(bmp, 0, 0, tW, tH);
		return bmp;
	}
	public static Bitmap zoomBitmap2(Context c,int source,int tW,int tH){
		Bitmap bmp=getBitmapFromResource(c,source);
		return zoomBitmap(bmp,tW,tH);
	}
	
	public static Bitmap zoomWidthBitmap(Bitmap source,int newWidth){
		int dstHeight = newWidth * source.getHeight()/ source.getWidth();
		return Bitmap.createScaledBitmap(source, newWidth, dstHeight, false);
	}
	
	public static Bitmap zoomBitmap(Context c,int source,int tW,int tH){
		Bitmap bmp=getBitmapFromResource(c,source);
		return Bitmap.createScaledBitmap(bmp, tW, tH, false);
	}
	
	public static Bitmap rotateBitmap(Bitmap source,float angle){
		Matrix mat=new Matrix();
		mat.setRotate(angle);
		return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), mat,false);
	}
	
	public static Bitmap getBitmapFromResource(Context c,int res){
		return BitmapFactory.decodeResource(c.getResources(), res);
	}
	
	public static void recycleBitmap(Bitmap bmp){
		if(bmp!=null){
			int size=bmp.getHeight()*bmp.getWidth()*3/1024;
			bmp.recycle();
			Log.e("recycleBitmap-------------",size+" KB");
			bmp=null;
		}
	}
	
	public static int getTransparentColor(int color ){
		int red=Color.red(color);
		int green=Color.green(color);
		int blue=Color.blue(color);
		return Color.argb(160, red, green, blue);
	}
	
	
}